import { legacy_createStore ,applyMiddleware } from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import { ALLTYPE ,CLASSIFYTYPE,STROETYPE ,ALLLIST,PROPSTYPE,CHILDRENTYPE} from '../Type/Store.d';


const initialState = {
    list:JSON.parse(localStorage.getItem('list') as any) || [],
    childrenList: JSON.parse(localStorage.getItem('childrenList') as any) || [],
    CopyList:[],
}

const routes =  (state:STROETYPE = initialState, { type, payload }:ALLTYPE) => {
    const Set_Item = (name:string,con:PROPSTYPE[])=>{
        window.localStorage.setItem(name,JSON.stringify(con));
    }
    switch (type) {

    case ALLLIST:
        state.list = payload;
        state.childrenList = state.list[0].children as PROPSTYPE[];
        state.CopyList = state.list[0].children as PROPSTYPE[];
        Set_Item('list',state.list)
        Set_Item('childrenList',state.childrenList)

        return { ...state}

    case CHILDRENTYPE:
        state.childrenList = state.list[payload].children as PROPSTYPE[];
        state.CopyList = state.list[payload].children as PROPSTYPE[];
        Set_Item('childrenList',state.childrenList)
        return { ...state}

    case CLASSIFYTYPE:
        let clasifyCopy = [...state.childrenList];
        clasifyCopy = state.CopyList.filter((item=>{
            return item.type == payload;
        }))
        state.childrenList = clasifyCopy
        return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}

    default:
        return state
    }
}
export default legacy_createStore(routes,applyMiddleware(thunk,logger))